package actions;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.StudentDBModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import data.Degree;
import data.StudentData;

public class FinalizeDegree extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		StudentData studentData = (StudentData) request.getSession().getAttribute("studentData");
		
		Degree currentDegree = (Degree) request.getSession().getAttribute("currentDegree");
		StudentDBModel db = new StudentDBModel();
		
		
		if(request.getParameter("degreeMajor").equals("other"))
		{
			String majorId = db.majorExists(request.getParameter("otherMajor"));
			
			if(majorId == null)
			{
			
				majorId = db.addMajor(request.getParameter("otherMajor"));
				
			}
			currentDegree.setMajor(request.getParameter("otherMajor"));
			currentDegree.setMajorID(majorId);
		}
		else
		{
			String mangledMajor = request.getParameter("degreeMajor");
			String[] tokens = new String[10];
			tokens = mangledMajor.split("#");
			currentDegree.setMajor(tokens[0]);
			currentDegree.setMajorID(tokens[1]);
		}
		
		currentDegree.setDate(request.getParameter("degreeDate"));
		currentDegree.setGpa(request.getParameter("degreeGPA"));
		currentDegree.setType(request.getParameter("degreeType"));
		
		studentData.appendToDegreeVector(currentDegree);
		
		Vector<String> formData = studentData.getFormData();
		
		request.getSession().setAttribute("formData", formData);
		request.getSession().setAttribute("studentData", studentData);
		request.getSession().setAttribute("currentDegree", currentDegree);
		
		return mapping.findForward("success");
	}

}
